﻿using System;
using System.Windows.Data;

namespace RLProject1.ViewModel.Helper {
    /// <summary>
    /// Konvertiert Strings zu Grad-Werten.
    /// <para>
    /// Dieser ValieConverter wird von der UserControl <see cref="GridFieldView"/> benutzt, um den Richtungspfeil entsprechend
    /// der ausgeführten Aktion (N, NO,...) auszurichten. Der Defaultwinkel von 0° zeigt nach NW. 
    /// </para>
    /// </summary>
    /// <example>
    /// <code>
    /// &lt;Canvas Name="Arrow" Width="30" Height="30" &gt;
    ///     &lt;Line X1="15" Y1="15" Stroke="{Binding ArrowBrush}" StrokeThickness="2"/&gt;
    ///     &lt;Line ...
    ///     &lt;Canvas.RenderTransform&gt;
    ///         &lt;RotateTransform x:Name="ArrowRotator" CenterX="17" CenterY="17" Angle="{Binding LastUpdated, Converter={StaticResource angelConverter}}" /&gt;
    ///     /lt;/Canvas.RenderTransform&gt;
    /// &lt;/Canvas&gt;
    /// </code>
    /// </example>
    [ValueConversion(typeof(String), typeof(double))]
    class StringActionToAngelConverter : IValueConverter {

        #region IValueConverter Members

        /// <summary>
        /// Converts the given value (must be String) to Double, default 0.
        /// </summary>
        /// <param name="value">Action as String (<see cref="GridActionTypes"/>)</param>
        /// <param name="targetType">ignored</param>
        /// <param name="parameter">ignored</param>
        /// <param name="culture">ignored</param>
        /// <returns>Double Value for a rotation in degrees</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            String action = (String)value;
            switch (action) {
                case "N":
                    return 45.0;
                case "NO":
                    return 90.0;
                case "O":
                    return 135.0;
                case "SO":
                    return 180.0;
                case "S":
                    return 225.0;
                case "SW":
                    return 270.0;
                case "W":
                    return 315.0;
                case "NW":
                    return 0.0;
                default:
                    return 0.0;
            }
        }

        /// <summary>
        /// Converts the given value (rotation) backs to <see cref="GridActionTypes"/> as String.
        /// </summary>
        /// <param name="value">Double value of the rotation in degrees</param>
        /// <param name="targetType">ignored</param>
        /// <param name="parameter">ignored</param>
        /// <param name="culture">ignored</param>
        /// <returns>String representation of the corresponding Action (<see cref="GridActionTypes"/>)</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            double angel = ((double)value * 10 % 3600) / 10.0;
            if (12.5 <= angel && angel < 57.5)
                return "N";
            if (57.5 <= angel && angel < 102.5)
                return "NO";
            if (102.5 <= angel && angel < 147.5)
                return "O";
            if (147.5 <= angel && angel < 192.5)
                return "SO";
            if (192.5 <= angel && angel < 237.5)
                return "S";
            if (237.5 <= angel && angel < 282.5)
                return "SW";
            if (282.5 <= angel && angel < 327.5)
                return "W";
            if ((327.5 <= angel && angel < 360) || (0 <= angel && angel < 12.5))
                return "NW";
            return null; //sollte nicht vorkommen
        }

        #endregion
    }
}
